import createAxios, { Options } from '@/utils/request'

const get = <T = any>(url: string, params: object = {}, options: Options = {}) => {
    return createAxios<T>(
        {
            url,
            method: 'GET',
            params,
        },
        options
    )
}

const post = <T = any>(url: string, data: object = {}, options: Options = {}) => {
    return createAxios<T>(
        {
            url,
            method: 'POST',
            data,
        },
        options
    )
}

const uploadFile = <T = any>(url: string, data: FormData, timeout: number = 1000 * 60) => {
    return createAxios<T>({
        url,
        method: 'POST',
        data,
        timeout,
        headers: {
            'Content-Type': 'multipart/form-data',
        },
    })
}

const downloadFile = <T = ArrayBuffer>(url: string, params: object = {}, method: 'GET' | 'POST' = 'GET') => {
    const config = {
        url,
        method,
        responseType: 'arraybuffer',
    } as any
    if (method === 'GET') {
        config.params = params
    } else {
        config.data = params
    }
    return createAxios<T>(config) as Promise<T>
}

export { get, post, uploadFile, downloadFile }
